package com.anmi.alfred.util;

import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CopyUtil {

  public static <T, K> List<T> copyList(List<K> sourceList, Class<T> clazz) {
    if (CollectionUtils.isEmpty(sourceList)) {
      return null;
    }

    ArrayList<T> target = Lists.newArrayList();
    sourceList.forEach(k -> target.add(convert(k, clazz)));
    return target;
  }

  public static <T, K> T convert(K source, Class<T> clazz) {
    T t = BeanUtils.instantiate(clazz);
    BeanUtils.copyProperties(source, t);
    return t;
  }
}
